package util

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

func SubString(s string, start, end int) string {
	souce := []rune(s)
	n := len(souce)
	if start < 0 || end > n || start > end {
		return ""
	}
	return string(souce[start:end])
}

func SortArrayStringBig2Small(array []string) []string {
	for i := 0; i < len(array)-1; i++ {
		for j := i; j < len(array); j++ {
			vi, _ := strconv.Atoi(array[i][1:])
			vj, _ := strconv.Atoi(array[j][1:])
			if vi < vj { //从大到小  从小到大 a[i]>a[j]
				array[i], array[j] = array[j], array[i]
			}
		}
	}
	return array
}

func SortArrayStringSmall2Big(array []string) []string {
	for i := 0; i < len(array)-1; i++ {
		for j := i; j < len(array); j++ {
			vi, _ := strconv.Atoi(array[i][1:])
			vj, _ := strconv.Atoi(array[j][1:])
			if vi > vj { //从大到小  从小到大 a[i]>a[j]
				array[i], array[j] = array[j], array[i]
			}
		}
	}
	return array
}

func SplitStringToUint32Slice(src string, sep string) []uint32 {
	strSlice := strings.Split(src, sep)
	var uint32Slice []uint32
	//uint32Slice := make([]uint32, 0, len(strSlice))
	for _, item := range strSlice {
		value, err := strconv.ParseUint(item, 10, 32)
		if err != nil {
			continue
		}
		uint32Slice = append(uint32Slice, uint32(value))
	}

	return uint32Slice
}

func Map2Array(m map[string]int) []string {
	arr := make([]string, 0)
	for k, _ := range m {
		arr = append(arr, k)
	}
	return arr
}

var random = rand.New(rand.NewSource(time.Now().UnixNano()))

func RandBetweenInt32(min, max int32) int32 {
	if min == max {
		return min
	}
	if min > max {
		min, max = max, min
	}
	return min + random.Int31n(max-min+1)
}
